package com.lmm.lcuser.lc_user.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClientOption;
import com.lmm.dresswisdom.lmmframe.util.LogUtils;
import com.lmm.lcuser.lc_user.utils.BDGPSUtils;
import com.lmm.lcuser.lc_user.utils.GPSReturn;


/**
 * @author xmgong
 * @date 2018/4/12
 * 后台位置服务
 */

public class LocationService extends Service {

    BDGPSUtils bdgpsUtils;

    @Override
    public void onCreate() {
        super.onCreate();
        if (bdgpsUtils == null) {
            bdgpsUtils = new BDGPSUtils(new gpsReturn(), this);
        }
    }

    /**
     * 重置定位参数
     *
     * @param span
     * @return
     */
    private LocationClientOption resetLocationOption(int span) {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
//        option.setOpenAutoNotifyMode(10000 , 1 , 1);
        option.setCoorType("bd09ll");
        option.setScanSpan(span);
        option.setIsNeedAddress(false);
        option.setOpenGps(true);
        option.setLocationNotify(false);
        option.setIsNeedLocationDescribe(true);
        option.setIsNeedLocationPoiList(true);
        option.setIgnoreKillProcess(false);
        option.SetIgnoreCacheException(false);
        option.setEnableSimulateGps(false);
        return option;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (bdgpsUtils != null) {
            bdgpsUtils.startGPS();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        bdgpsUtils.stopGPS();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    class gpsReturn implements GPSReturn {
        @Override
        public void retrunGPSInfo(BDLocation bdLocation) {
            if (bdLocation != null) {
                LogUtils.write("lat:" + bdLocation.getLatitude());
                LogUtils.write("lon:" + bdLocation.getLongitude());
            }
        }
    }
}
